package com.haikesi.api.role.service.impl;

import com.haikesi.api.base.Constants;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.ErrorTools;
import com.haikesi.api.base.Type;
import com.haikesi.api.role.dao.RoleAuthorityDao;
import com.haikesi.api.role.dao.RoleDao;
import com.haikesi.api.role.domain.po.RoleAuthorityPO;
import com.haikesi.api.role.domain.po.RolePO;
import com.haikesi.api.role.domain.vo.*;
import com.haikesi.api.role.service.IRoleService;
import com.haikesi.api.user.domain.po.UserPO;
import com.haikesi.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RoleServiceImpl
 * @Author HandsomeBugs
 * @Date 2019/4/30 9:33
 * @Version 1.0
 **/
@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleAuthorityDao roleAuthorityDao;

    @Override
    public List<RoleVO> getList(RoleSearchVO searchVO) {
        return roleDao.getList(searchVO);
    }

    @Override
    public void delete(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.FORMAT_EXCEPTION);
        }
        RolePO rolePO = roleDao.selectByPrimaryKey(id);
        if (rolePO == null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR, ErrorTools.ErrorAsArrayList(new Error("id", "角色不存在")));
        }
        RolePO po = new RolePO();
        po.setId(id);
        po.preDelete();
        int i = roleDao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
    }

    @Override
    public void update(RoleUpdateVO updateVO) {
        RolePO po = new RolePO();
        BeanUtils.copyProperties(updateVO, po);
        po.preUpdate();
        int i = roleDao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
    }

    @Override
    public String create(RoleSaveVO saveVO) {
        RolePO po = new RolePO();
        BeanUtils.copyProperties(saveVO, po);
        po.preInsert();
        int i = roleDao.insertSelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
        return po.getId();
    }

    @Override
    public void addAuthority(RoleAuthorityVO roleAuthorityVO) {
        if (roleAuthorityVO.getAuthorityIds().size() < 1) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        RoleAuthorityPO po =new RoleAuthorityPO();

        Example example = new Example(RoleAuthorityPO.class);
        example.and().andEqualTo("roleId",roleAuthorityVO.getRoleId());
        roleAuthorityDao.deleteByExample(example);
        po.setRoleId(roleAuthorityVO.getRoleId());
        for (String authorityId : roleAuthorityVO.getAuthorityIds()) {
            po.setAuthorityId(authorityId);
            roleAuthorityDao.insertSelective(po);
        }
    }

    @Override
    public List<String> getAuthority(String roleId) {
        List<String> rList=new ArrayList<>();
        Example example = new Example(RoleAuthorityPO.class);
        example.and().andEqualTo("roleId",roleId);
        List<RoleAuthorityPO> roleAuthorityPos=roleAuthorityDao.selectByExample(example);
        if(roleAuthorityPos==null){
            return rList;
        }
        for (RoleAuthorityPO po:roleAuthorityPos) {
            rList.add(po.getAuthorityId());
        }
        return rList;
    }

}
